package app.cryp;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

public class RSAUtils {

	public static final String KEY_ALGORTHM = "RSA";
	//所谓的私钥
	public static final String PRIVATE_KEY_HEX = "30820276020100300d06092a864886f70d0101010500048202603082025c0201000281810088ec27f586d7b1edffc49f055066664687a223699ec088145f8c8a8e6c5413e38d82d63a42817774c8a2c4773460481e956c20a875ed01bda595cda9c16c82aa564264eb40cad411b02ba84b83d43c15a0153e1ea513eb08f751cbaa35768ca24b4bf556b1f519d261a1f1aef62445b9a58a69d2e8593fa4dfa47ca27357e98d020301000102818007ff6346201e313269a21e77bcfe7e32d8714c083b519ba135038c844d278c11da8e2ddd314d46a0f7a72a3a9c409375d5dc7efbd80783f683d9802dce327c5f483f554379220a5511799bc535bb9e4035b7e060a46dd50c33cb8fe7896bcaca120345b263feed8cd71a89bf98057e3c538404abde3b9aadb8f5f5c24677ff81024100eb85ded5583b0993ac870b118cd1a9eb3a5892900a4f954b349c8640c6252a20b8a0072eadbf809c251839dd6a065fa4ea3ea342c4575462926246048349a7f502410094d3b2092cf0ee213426450792e89fd1db2a34c04e5cfaebfb6f349e944734a37dfbeed8fd6f0216a0a0219b64b8a1bf037fa89c4065e76cdbdbd870b6ecf439024044461c6090211d1797edd5c192a6bcb119cacf41be1b6fd5cb3c8c967e5a0baa4c9ae73a1ad7e6ba5cee76e7be72768b61f422739a630e6d3cae0c4a069cf57902400e9a0e97b3df179e0e5dcadc095fe57e53807a987b97c7ceab66a7c4ff4b5530cd4590939d99ec9c87b9ee522eb9a6b4f504eb681931665959e0e2057d0d1691024100b1183168b8236bff56ce7ed468f036cdb1bedf2f4c46b6c343076c583e4dc697b0d267969cbd0a640a3c5df23a0aba8bf049ea14b214442636d63a81e063dd99";

	public static PrivateKey PRIVATE_KEY;
	static {
		try {
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
					HexUtils.hexToBytes(PRIVATE_KEY_HEX));
			KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
			PRIVATE_KEY = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	public static final BouncyCastleProvider BOUNCY_CASTLE_PROVIDER = new org.bouncycastle.jce.provider.BouncyCastleProvider();

	public static String decryptByPrivateKey(byte[] content) throws Exception {

		// 对数据解密
		Cipher cipher = Cipher
				.getInstance(KEY_ALGORTHM, BOUNCY_CASTLE_PROVIDER);
		cipher.init(Cipher.DECRYPT_MODE, PRIVATE_KEY);
		return new String(cipher.doFinal(content));
	}
}
